<template>
  <div class="add-edit">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="yuyi-ruleForm">

      <el-form-item label="角色名称" prop="roleName">
        <el-input :disabled="type==='see'" size="small" v-model="ruleForm.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色代码" prop="roleCode">
        <el-input :disabled="type==='see'" size="small" v-model="ruleForm.roleCode"></el-input>
      </el-form-item>
      <el-form-item label="角色序号" prop="orderNo">
        <el-input :disabled="type==='see'" size="small" v-model="ruleForm.orderNo"></el-input>
      </el-form-item>

      <el-form-item label="可用" prop="isEnable">
        <el-switch :disabled="type==='see'" v-model="ruleForm.isEnable"></el-switch>
      </el-form-item>
      <el-form-item label="系统角色" prop="isSysRole">
        <el-switch :disabled="type==='see'" v-model="ruleForm.isSysRole"></el-switch>
      </el-form-item>
      <el-form-item label="备注">
        <el-input size="small"
                  type="textarea"
                  :disabled="type==='see'"
                  show-word-limit
                  maxlength="200"
                  :autosize="autosize"
                  v-model="ruleForm.remark"></el-input>
      </el-form-item>
      <el-form-item v-if="type!=='see'">
        <el-button type="primary"
                   @click="handleSubmit"
                   :loading="loading"
                   size="small">{{ type === 'add' ? '确定新增' : '确定修改'}}</el-button>
        <el-button style="margin-left: 10px" size="small" @click="handleEmpty">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addRole, editRole, seeRole } from '../../../../../api/user/userSet'

export default {
  name: 'addEdit',
  props: {
    type: {
      type: String,
      default: () => 'add'
    },
    editData: {
      type: Object
    }
  },
  data () {
    return {
      autosize: {
        minRows: 4,
        maxRows: 6
      },
      loading: false,
      ruleForm: {
        roleName: '',
        roleCode: '',
        isEnable: false,
        orderNo: '',
        isSysRole: false,
        remark: ''
      },
      rules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }
        ],
        roleCode: [
          {
            required: true,
            message: '请输入角色代码',
            trigger: 'blur'
          }
        ],
        orderNo: [
          {
            required: true,
            message: '请输入角色序号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted () {
    if (this.type === 'add') {
      this.handleEmpty()
    } else {
      this.handleSet()
    }
  },
  watch: {
    type () {
      if (this.type === 'add') {
        this.handleEmpty()
      } else {
        this.handleSet()
      }
    }
  },
  methods: {
    handleSet () {
      seeRole(this.editData.id).then(res => {
        Object.keys(this.ruleForm).forEach((key) => {
          this.ruleForm[key] = res.data.data[key]
        })
        this.ruleForm.id = res.data.data.id
      })
    },
    handleSubmit () {
      // 提交
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.type === 'add') {
            delete this.ruleForm.id
          }
          const result = this.type === 'add' ? addRole(this.ruleForm) : editRole(this.ruleForm)
          result.then(res => {
            this.handleEmpty()
            this.$message({
              message: '新增角色信息成功',
              type: 'success'
            })
            this.$emit('handleSuccess')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    handleEmpty () {
      // 清空内容
      this.ruleForm.remark = ''
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
  .add-edit {
    padding-right: 40px;
  }
</style>
